package com.hxrfid.web.controller.project;

import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.annotation.RepeatSubmit;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.project.domain.bo.BsCardBo;
import com.hxrfid.project.domain.vo.BsCardVo;
import com.hxrfid.project.service.IBsCardService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 卡片管理
 *
 * @author hxrfid
 * @date 2025-06-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/card")
public class BsCardController extends BaseController {

    private final IBsCardService iBsCardService;

    /**
     * 查询卡片管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<BsCardVo> list(BsCardBo bo, PageQuery pageQuery) {
        return iBsCardService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出卡片管理列表
     */
    @Log(title = "卡片管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsCardBo bo, HttpServletResponse response) {
        List<BsCardVo> list = iBsCardService.queryList(bo);
        ExcelUtil.exportExcel(list, "卡片管理", BsCardVo.class, response);
    }

    /**
     * 获取卡片管理详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BsCardVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBsCardService.queryById(id));
    }

    /**
     * 新增卡片管理
     */
    @Log(title = "卡片管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsCardBo bo) {
        return toAjax(iBsCardService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改卡片管理
     */
    @Log(title = "卡片管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsCardBo bo) {
        return toAjax(iBsCardService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除卡片管理
     *
     * @param ids 主键串
     */
    @Log(title = "卡片管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBsCardService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

}
